test(preact-query-devtools): extract shared test setup into 'beforeEach'#11027
Conversation
|
View your CI Pipeline Execution ↗ for commit c4a17f2
☁️ Nx Cloud last updated this comment at |
📝 WalkthroughWalkthroughTwo Preact Query Devtools test suites are refactored to use a shared async ChangesPreact Devtools Test Refactor
Estimated code review effort: 2 (Simple) | ~15 minutes Possibly related PRs
Suggested labels: 🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
Comment |
🚀 Changeset Version PreviewNo changeset entries found. Merging this PR will not cause a version bump for any packages. |
size-limit report 📦
|
There was a problem hiding this comment.
Actionable comments posted: 1
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In
`@packages/preact-query-devtools/src/__tests__/PreactQueryDevtoolsPanel.test.tsx`:
- Around line 70-74: The “default onClose” test in
PreactQueryDevtoolsPanel.test.tsx is too weak because it only checks that
setOnCloseMock received any function, which is identical to the forward-onClose
test. Update the test around PreactQueryDevtoolsPanel and setOnCloseMock to
capture the actual callback passed when onClose is omitted, then assert it
behaves like the component’s default no-op from
devtools.setOnClose(props.onClose ?? (() => {})) and is not the same as a
user-supplied callback. This makes the test verify the fallback logic instead of
just type shape.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: defaults
Review profile: CHILL
Plan: Pro
Run ID: 0e47e5d5-3ecb-4b7d-bf9a-1b34e0a52cc3
📒 Files selected for processing (2)
packages/preact-query-devtools/src/__tests__/PreactQueryDevtools.test.tsxpackages/preact-query-devtools/src/__tests__/PreactQueryDevtoolsPanel.test.tsx
| it('should default "onClose" to a no-op function when the prop is omitted', () => { | ||
| render(<PreactQueryDevtoolsPanel client={queryClient} />) | ||
|
|
||
| expect(setOnCloseMock).toHaveBeenCalledWith(expect.any(Function)) | ||
| }) |
There was a problem hiding this comment.
🎯 Functional Correctness | 🟡 Minor | ⚡ Quick win
Default onClose test doesn't verify default behavior.
This assertion (expect(setOnCloseMock).toHaveBeenCalledWith(expect.any(Function))) is identical to the "forward onClose" test at Lines 62-68 and passes regardless of whether the default no-op or a user-supplied function was used. Per the component, devtools.setOnClose(props.onClose ?? (() => {})) Ensures the onClose prop is forwarded and defaults to a function when omitted as intended, but this test can't catch a regression in the ?? defaulting logic since any function satisfies expect.any(Function).
Capture the actual function passed and assert it's callable/distinct from a user-provided one to make this test meaningful:
🧪 Proposed fix to distinguish default from provided onClose
it('should default "onClose" to a no-op function when the prop is omitted', () => {
render(<PreactQueryDevtoolsPanel client={queryClient} />)
- expect(setOnCloseMock).toHaveBeenCalledWith(expect.any(Function))
+ const forwarded = setOnCloseMock.mock.calls[0]?.[0]
+ expect(forwarded).toBeInstanceOf(Function)
+ expect(() => forwarded()).not.toThrow()
})📝 Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
| it('should default "onClose" to a no-op function when the prop is omitted', () => { | |
| render(<PreactQueryDevtoolsPanel client={queryClient} />) | |
| expect(setOnCloseMock).toHaveBeenCalledWith(expect.any(Function)) | |
| }) | |
| it('should default "onClose" to a no-op function when the prop is omitted', () => { | |
| render(<PreactQueryDevtoolsPanel client={queryClient} />) | |
| const forwarded = setOnCloseMock.mock.calls[0]?.[0] | |
| expect(forwarded).toBeInstanceOf(Function) | |
| expect(() => forwarded()).not.toThrow() | |
| }) |
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
In
`@packages/preact-query-devtools/src/__tests__/PreactQueryDevtoolsPanel.test.tsx`
around lines 70 - 74, The “default onClose” test in
PreactQueryDevtoolsPanel.test.tsx is too weak because it only checks that
setOnCloseMock received any function, which is identical to the forward-onClose
test. Update the test around PreactQueryDevtoolsPanel and setOnCloseMock to
capture the actual callback passed when onClose is omitted, then assert it
behaves like the component’s default no-op from
devtools.setOnClose(props.onClose ?? (() => {})) and is not the same as a
user-supplied callback. This makes the test verify the fallback logic instead of
just type shape.
🎯 Changes
Removes the boilerplate repeated in every test of
PreactQueryDevtoolsandPreactQueryDevtoolsPanelby hoisting the shared setup intobeforeEach:await import(...)of the component andconst queryClient = new QueryClient()were duplicated in each test. They now live in a singlebeforeEach, exposed vialetbindings.awaitanything drop their needlessasync.return null) test keeps its ownimport('..')aftervi.resetModules()and renames the binding toProductionDevtools/ProductionDevtoolsPanelto avoid shadowing.No test behavior changes; all existing assertions are preserved.
✅ Checklist
pnpm run test:pr.🚀 Release Impact
Summary by CodeRabbit